package com.yixing.tech.wcs.service.impl;

import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.tcp.manage.EndpointManager;
import com.yixing.tech.wcs.domain.po.Task;
import com.yixing.tech.wcs.service.IBoxInteractionScheduleService;
import com.yixing.tech.wcs.service.IBoxTaskService;
import com.yixing.tech.wcs.service.ICRUDService;
import com.yixing.tech.wcs.tcp.Command;
import com.yixing.tech.wcs.tcp.EndpointConfiguration;
import com.yixing.tech.wcs.tcp.InteractionState;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class BoxInteractionScheduleServiceImpl implements IBoxInteractionScheduleService {

    @Resource
    EndpointManager endpointManager;

    @Resource
    ICRUDService crudService;

    @Resource
    IBoxTaskService boxTaskService;

    @Override
    public void queryStateForPutDownBox() {
        for(Map.Entry<String, InteractionState> entry: BoxTaskService.podInteractionStateMap.entrySet()) {
            if(InteractionState.PREPARED.equals(entry.getValue())) {
                endpointManager.sendMsgByTcpClientEndpoint(EndpointConfiguration.getEndpointId(), Command.ClientCommand.QUERY_STATUS);
                break;
            }
        }
    }

    @Override
    public void queryStateForTakeOutBox() {
        for(Map.Entry<String, InteractionState> entry: BoxTaskService.podInteractionStateMap.entrySet()) {
            if(InteractionState.PREPARED_TAKE_OUT.equals(entry.getValue())) {
                endpointManager.sendMsgByTcpClientEndpoint(EndpointConfiguration.getEndpointId(), Command.ClientCommand.QUERY_STATUS);
                break;
            }
        }
    }

    @Override
    public void queryDeviceState() {
        for(Map.Entry<String, InteractionState> entry: BoxTaskService.podInteractionStateMap.entrySet()) {
            if(InteractionState.PREPARED_TAKE_OUT.equals(entry.getValue()) || InteractionState.PREPARED.equals(entry.getValue())) {
                endpointManager.sendMsgByTcpClientEndpoint(EndpointConfiguration.getEndpointId(), Command.ClientCommand.QUERY_STATUS);
                break;
            }
        }
    }

    @Override
    public void queryCameraPodCode() {
        for(Map.Entry<String, InteractionState> entry: BoxTaskService.podInteractionStateMap.entrySet()) {
            if(InteractionState.PUT_DOWN_NOTIFIED.equals(entry.getValue()) || InteractionState.PREPARED.equals(entry.getValue())) {
                endpointManager.sendMsgByTcpClientEndpoint(EndpointConfiguration.getEndpointId(), Command.ClientCommand.QUERY_STATUS);
                break;
            }
        }
    }

    @Override
    public void notifyBoxPutDownFin() {
        for(Map.Entry<String, InteractionState> entry: BoxTaskService.podInteractionStateMap.entrySet()) {
            if(InteractionState.PUT_DOWN_FINISHED.equals(entry.getValue())) {
                endpointManager.sendMsgByTcpClientEndpoint(EndpointConfiguration.getEndpointId(), Command.ClientCommand.FIN_PUT_DOWN);
                break;
            }
        }
    }

    @Override
    public void notifyBoxHandlingFin() {
        for(Map.Entry<String, InteractionState> entry: BoxTaskService.podInteractionStateMap.entrySet()) {
            if(InteractionState.HANDLING_FINISHED.equals(entry.getValue())) {
                endpointManager.sendMsgByTcpClientEndpoint(EndpointConfiguration.getEndpointId(), Command.ClientCommand.FIN_HANDLE);
                break;
            }
        }
    }

    @Override
    public void notifyBoxTakeOutFin() {
        for(Map.Entry<String, InteractionState> entry: BoxTaskService.podInteractionStateMap.entrySet()) {
            if(InteractionState.PICK_UP_FINISHED.equals(entry.getValue())) {
                endpointManager.sendMsgByTcpClientEndpoint(EndpointConfiguration.getEndpointId(), Command.ClientCommand.FIN_PICK_UP);
                break;
            }
        }
    }

    @Override
    public void boxHandlingFin() {
        List<Task> taskList = crudService.queryTaskUnIssuedOrIssued();
        for(Task task: taskList) {
            if(ObjectUtils.isNotEmpty(task.getPodCode())
                    && BoxTaskService.podInteractionStateMap.containsKey(task.getPodCode())
                    && InteractionState.PUT_DOWN_NOTIFIED.equals(BoxTaskService.podInteractionStateMap.get(task.getPodCode()))) {
                try {
                    boxTaskService.boxHandlingFinish(task.getPodCode());
                } catch (BusinessException e) {
                    log.error("更新料箱完成处理逻辑抛出异常:{}", e);
                }
            }
        }
    }
}
